//
//  DYShopSXListView.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/6/30.
//  Copyright © 2018年 ZDQK. All rights reserved.
// 店铺筛选点击的下拉菜单

#import "DYShopSXListView.h"
#import <Masonry/Masonry.h>

@interface DYShopSXListViewCell : UITableViewCell

@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UIImageView * rightImgView;

- (void)loadData:(id)data isSelected:(BOOL)isSelected;

@end

@implementation DYShopSXListViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.nameLabel = [UILabel new];
        [self addSubview:self.nameLabel];
        self.nameLabel.font = [UIFont systemFontOfSize:14];
        self.nameLabel.textColor = [UIColor blackTextColor];
        self.rightImgView = [[UIImageView alloc] init];
        self.rightImgView.image = [UIImage imageNamed:@"er_right"];
        [self addSubview:self.rightImgView];
        [self _layout];
    }
    return self;
}

- (void)_layout{
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.height.equalTo(@40);
        make.left.equalTo(self).offset(12);
    }];
    [self.rightImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-12);
        make.centerY.equalTo(self);
    }];
}

- (void)loadData:(id)data isSelected:(BOOL)isSelected{
    if (isSelected) {
        self.rightImgView.hidden = NO;
        self.nameLabel.textColor = [UIColor colorWithHex:0xda343b alpha:1];
    }else{
        self.rightImgView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackTextColor];
    }
    self.nameLabel.text = data;
}

@end

////////////////////////////////////////////////////
///////////////////////////////////////////////////


@interface DYShopSXListView()<UITableViewDelegate,UITableViewDataSource>{
    NSArray * _titles;
}

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) UIControl * bgContrl;
@property (nonatomic,strong) UIView * tableFooterBg;
@property (nonatomic,copy) void(^didSelectedIndex)(id index);

@end


@implementation DYShopSXListView


- (instancetype)init{
    if (self = [super init]) {
        self.tableFooterBg = [UIView new];
        self.tableFooterBg.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
        [self addSubview:self.tableFooterBg];
        self.bgContrl = [UIControl new];
        [self.bgContrl addTarget:self action:@selector(bgTouched) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.bgContrl];
//        self.bgContrl.backgroundColor =
        [self.bgContrl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        self.tableView = [[UITableView alloc] init];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[DYShopSXListViewCell class] forCellReuseIdentifier:@"cell"];
        [self addSubview:self.tableView];
        _titles = @[@"全部商品",@"自营",@"大卖场"];
        self.currentSelected = -1;
    }
    return self;
}

- (void)bgTouched{
    [self hide];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DYShopSXListViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    BOOL isSelected = NO;
    if (self.currentSelected == indexPath.row) {
        isSelected = YES;
    }
    [cell loadData:_titles[indexPath.row] isSelected:isSelected];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titles.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.currentSelected == indexPath.row) {
        self.currentSelected = 0;
    }else{
        self.currentSelected = indexPath.row;
    }
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

+ (DYShopSXListView *)showInView:(UIView *)view WithRect:(CGRect)rect currentSelected:(NSInteger )currentSelected callback:(void (^)(id))callback{
    DYShopSXListView * listView = [[DYShopSXListView alloc] init];
    [[UIApplication sharedApplication].keyWindow addSubview:listView];
    listView.frame = [UIScreen mainScreen].bounds;
    listView.currentSelected = currentSelected;
    listView.tableView.frame = CGRectMake(0, rect.origin.y + rect.size.height, ScreenWidth, 120);
    [listView.tableFooterBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(listView);
        make.top.equalTo(listView.tableView.mas_bottom);
    }];
    listView.alpha = 0;
    listView.didSelectedIndex = callback;
    [UIView animateWithDuration:.3 animations:^{
        listView.alpha = 1;
    }];
    return listView;
}


- (void)hide{
    self.didSelectedIndex(@(self.currentSelected));
    [UIView animateWithDuration:.3 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        
    }];
}

@end
